package com.mars.foodserver.controller.common;

import com.mars.foodserver.exception.WorkException;
import com.mars.foodserver.response.result.R;
import com.mars.foodserver.service.IUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author wangqiang
 * @version 1.0
 * @date 2021/3/25 22:54
 * 阿里云文件上传
 */
@RestController
@Slf4j
@RequestMapping("/upload")
public class UploadController {

    @Resource
    private IUploadService uploadService;


    /**
     * 上传图片
     *
     * @param multipartFile multipartFile
     * @return R
     */
    @RequestMapping(value = "/img", method = RequestMethod.POST)
    private R upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new WorkException("请选择文件");
        }
        String path = uploadService.uploadImage(multipartFile);
        log.info("文件上传路径:" + path);
        return R.success(path);
    }


}
